import fasttext
import numpy as np
import scipy
import scipy.spatial
from scripts_for_figure_2_and_table_2 import *

def face_validity_scores(political_hostility, model_type, w_many = 20):    
    
    all_results = []
    model = fasttext.load_model("model_facebook.bin")
    vocabulary = model.words

    all_words = []
    
    for vector in political_hostility:
        
        if type(vector)==list:
            vector_distance = vector_normalize(np.sum([model[element] for element in vector], axis = 0))
            all_words.append(" ".join(vector).capitalize())
        else:
            vector_distance = vector_normalize(model[vector])
            all_words.append(vector.capitalize())
    
        distances_for_political_hate = []
        words_for_political_hate = []
        
        for word in vocabulary:
            distance = (1-scipy.spatial.distance.cosine(vector_normalize(model[word]),vector_distance))
            distances_for_political_hate.append((distance,word))

        political_hate_common = pd.DataFrame(distances_for_political_hate, columns = ["distance","word"])
        political_hate_common = political_hate_common.sort_values("distance", ascending = False).iloc[0:20000].reset_index(drop=True)
        political_hate_common["both"] = "(" + political_hate_common["distance"].round(2).astype("str") + "," + " " + political_hate_common["word"] + ")"
        
        all_results.append(political_hate_common["both"])
    
    final_results = pd.concat(all_results, axis = 1, ignore_index=True)
    final_results.columns = all_words
    
    return(final_results)